package com.farm.authority.action;

import com.farm.authority.domain.Post;
import com.farm.authority.service.OrganizationServiceInter;

import com.farm.web.WebSupport;
import com.farm.web.easyui.EasyUiTreeNode;
import com.farm.web.easyui.EasyUiUtils;

import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.farm.core.page.CommitType;
import com.farm.core.page.PageSet;
import com.farm.core.page.PageType;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBSort;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.web.spring.BeanFactory;

/* *
 *功能：岗位控制层
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20141124152033
 *说明：
 */
public class PostAction extends WebSupport {
	private Map<String, Object> jsonResult;// 结果集合
	private DataQuery query;// 条件查询
	private Post entity;// 实体封装
	private PageSet pageset;// 请求状态
	private String ids;// 主键集合
	private String id;// 主键集合
	private List<EasyUiTreeNode> treeNodes;// 异步树的封装

	/**
	 * 查询结果集合
	 * 
	 * @return
	 */
	public String queryall() {
		try {
			query = EasyUiUtils.formatGridQuery(getRequest(), query);
			query.addRule(new DBRule("ORGANIZATIONID", ids, "="));
			DataResult result = organizationIMP.createPostSimpleQuery(query)
					.search();
			result.runDictionary("1:标准岗位,2:临时岗位", "TYPE");
			jsonResult = EasyUiUtils.formatGridData(result);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	/**
	 * 提交修改数据
	 * 
	 * @return
	 */
	public String editSubmit() {
		try {
			entity = organizationIMP.editPost(entity.getId(), entity.getName(),
					entity.getType(), getCurrentUser());
			pageset = new PageSet(PageType.UPDATE, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.UPDATE,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 提交新增数据
	 * 
	 * @return
	 */
	public String addSubmit() {
		try {
			entity = organizationIMP.insertPost(ids, entity.getName(), entity
					.getType(), getCurrentUser());
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.ADD,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}
	/**
	 * 设置角色权限树
	 * 
	 * @return
	 */
	public String roleGroupSetTree() {
		try {
			List<String> actions = parseIds(ids);
			organizationIMP.setPostActionTree(actions, id);
			pageset = new PageSet(PageType.OTHER, CommitType.TRUE);
		} catch (Exception e) {
			log.error(e.getMessage());
			pageset = new PageSet(PageType.OTHER, CommitType.FALSE);
		}
		return SUCCESS;
	}
	/**
	 * 删除数据
	 * 
	 * @return
	 */
	public String delSubmit() {
		try {
			for (String id : parseIds(ids)) {
				organizationIMP.deletePostEntity(id, getCurrentUser());
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 跳转
	 * 
	 * @return
	 */
	public String forSend() {
		return SUCCESS;
	}

	/**
	 * 构造岗位权限树
	 * 
	 * @return
	 */
	public String postActionsTreeInit() {
		treeNodes = EasyUiTreeNode.formatAjaxTree(initRoleGruopTreeNode(),
				"A_PARENTID", "A_ID", "A_NAME", "C_POSTID","A_ICON");
		return SUCCESS;
	}

	/**
	 * 查询菜单权限构造树形菜单
	 * 
	 * @return
	 */
	private List<Map<String, Object>> initRoleGruopTreeNode() {
		DataResult result = null;
		try {
			query = DataQuery
					.init(
							query,
							"alone_auth_actiontree A LEFT JOIN alone_auth_action B ON A.ACTIONID = B.ID LEFT JOIN (SELECT POSTID, MENUID FROM alone_auth_postaction WHERE POSTID = '"
									+ ids + "') C ON A.id = C.menuid",
							"A.NAME,A.DOMAIN,a.ICON,A.ID,A.SORT,A.PARENTID,A.TREECODE,B.AUTHKEY,A.name as B_LABLE,A.TYPE,c.postid");
			query.setDistinct(true);
			query.setPagesize(100);
			query.addSort(new DBSort(" a.DOMAIN,LENGTH(A.TREECODE),a.SORT",
					"ASC", false));
			query.setNoCount();
			result = query.search();
			for (Map<String, Object> node : result.getResultList()) {
				node.put("A_NAME", node.get("A_NAME").toString() + "&nbsp;&nbsp;&nbsp;&nbsp;<span style='color:gray;font-size:10px;'>"
						+ (node.get("A_DOMAIN").toString()).toUpperCase()+"</span>");
			}
		} catch (Exception e) {
			result = DataResult.setMessager(result, e + e.getMessage());
			log.error(e.getMessage() + e);
		}
		return result.getResultList();
	}

	/**
	 * 显示详细信息（修改或浏览时）
	 * 
	 * @return
	 */
	public String view() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			switch (pageset.getPageType()) {
			case (1): {// 新增
				return SUCCESS;
			}
			case (0): {// 展示
				entity = organizationIMP.getPostEntity(ids);
				return SUCCESS;
			}
			case (2): {// 修改
				entity = organizationIMP.getPostEntity(ids);
				return SUCCESS;
			}
			default:
				break;
			}
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.OTHER,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	private final static OrganizationServiceInter organizationIMP = (OrganizationServiceInter) BeanFactory
			.getBean("farm_authority_Organization_ProxyId");

	// ----------------------------------------------------------------------------------
	public DataQuery getQuery() {
		return query;
	}

	public void setQuery(DataQuery query) {
		this.query = query;
	}

	public Post getEntity() {
		return entity;
	}

	public void setEntity(Post entity) {
		this.entity = entity;
	}

	public PageSet getPageset() {
		return pageset;
	}

	public void setPageset(PageSet pageset) {
		this.pageset = pageset;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Map<String, Object> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, Object> jsonResult) {
		this.jsonResult = jsonResult;
	}

	public List<EasyUiTreeNode> getTreeNodes() {
		return treeNodes;
	}

	public void setTreeNodes(List<EasyUiTreeNode> treeNodes) {
		this.treeNodes = treeNodes;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	private static final Logger log = Logger.getLogger(PostAction.class);
	private static final long serialVersionUID = 1L;
}
